package com.sourceQ.system.controller.message.mail;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sourceQ.common.core.domain.R;
import com.sourceQ.common.core.web.controller.BaseController;
import com.sourceQ.common.core.web.domain.AjaxResult;
import com.sourceQ.common.log.annotation.Log;
import com.sourceQ.common.log.enums.BusinessType;
import com.sourceQ.common.security.utils.SecurityUtils;
import com.sourceQ.system.domain.message.mail.SysMailTemplate;
import com.sourceQ.system.dto.message.MailTemplatePageReqDTO;
import com.sourceQ.system.dto.message.MailTemplateSaveReqDTO;
import com.sourceQ.system.dto.message.MailTemplateSendReqDTO;
import com.sourceQ.system.service.message.mail.SysMailSendService;
import com.sourceQ.system.service.message.mail.SysMailTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author: SourceQ
 * @create: 2024-04-28 17:14
 * @Description:
 */
@RestController
@RequestMapping("/mail-template")
@Api(tags = "邮件模板管理")
public class MailTemplateController extends BaseController {
    @Autowired
    private SysMailTemplateService mailTemplateService;
    @Autowired
    private SysMailSendService mailSendService;
    @ApiOperation(value = "创建邮件模版")
    @PostMapping("/create")
    @Log(title = "邮件模板管理",businessType = BusinessType.INSERT)
    public AjaxResult create(@Valid @RequestBody MailTemplateSaveReqDTO createReqVO){
        return success(mailTemplateService.createMailTemplate(createReqVO));
    }

    @ApiOperation(value = "修改邮件模版")
    @PutMapping("/update")
    @Log(title = "邮件模板管理",businessType = BusinessType.UPDATE)
    public AjaxResult update(@Valid @RequestBody MailTemplateSaveReqDTO updateReqVO){
        mailTemplateService.updateMailTemplate(updateReqVO);
        return success();
    }

    @ApiOperation(value = "删除邮件模版")
    @Log(title = "邮件模板管理",businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable Long id){
        mailTemplateService.deleteMailTemplate(id);
        return success();
    }
    @ApiOperation(value = "查询邮件模板")
    @Log(title = "邮件模板管理",businessType = BusinessType.QUERY)
    @GetMapping("/page")
    public R<IPage<SysMailTemplate>> page(Page page, MailTemplatePageReqDTO dto){
        return R.ok(mailTemplateService.getMailTemplatePage(page, dto));
    }
    @ApiOperation(value = "邮件模板详情")
    @Log(title = "邮件模板管理",businessType = BusinessType.QUERY)
    @GetMapping("/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        return success(mailTemplateService.getMailTemplate(id));
    }
    @ApiOperation(value = "邮件发送")
    @Log(title = "邮件模板管理",businessType = BusinessType.OTHER)
    @PostMapping("/send-mail")
    public AjaxResult send(@Valid @RequestBody MailTemplateSendReqDTO sendReqVO){
        return success(mailSendService.sendSingleMail(sendReqVO.getMail(), SecurityUtils.getUserId(),0,
                sendReqVO.getTemplateCode(),sendReqVO.getTemplateParams()));
    }

}
